package com.xhx.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Core 核心 数据库数据源配置
 */
@Configuration
@MapperScan(basePackages = MySqlDataSourceConfig.ENTITY_PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MySqlDataSourceConfig {

    /**
     * 指定 Mapper 连接字符串
     */
    static final String DATASOURCE_PREFIX = "spring.datasource.mysql";
    static final String DATASOURCE_PREFIX2 = "spring.datasource.mysql2";

    /**
     * 指定 Mapper 接口类目录
     */
    static final String ENTITY_PACKAGE = "com.xhx.dao.alluse";

    /**
     * 指定 Mapper 映射文件目录
     */
    //static final String MAPPER_PATH = "classpath:mybatis/*.xml";
    static final String MAPPER_PATH = "classpath:mysql/*.xml";

    /**
     * 设置数据源
     *
     * @return 数据源
     */
    @Bean(name = "mysqlDataSource")
    @Qualifier("mysqlDataSource")
    @ConfigurationProperties(prefix = DATASOURCE_PREFIX) // 连接字符串
    @Primary
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mysql2DataSource")
    @ConfigurationProperties(prefix = DATASOURCE_PREFIX2)
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    /**
     * JDBC模板
     *
     //* @param dataSource 数据源
     //* @return 引擎
     */
    //@Bean(name = "mysqlJdbcTemplate")
    //@Primary
    //public JdbcTemplate setJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
    //    return new JdbcTemplate(dataSource);
    //}
    //@Bean(name = "mysql2JdbcTemplate")
    //public JdbcTemplate setJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
    //    return new JdbcTemplate(dataSource);
    //}


    @Bean(name = "dynamicDataSource")
    public DynamicDataSource DataSource(@Qualifier("mysqlDataSource") DataSource primaryDataSource,
                                        @Qualifier("mysql2DataSource") DataSource secondaryDataSource) {

        //这个地方是比较核心的targetDataSource 集合是我们数据库和名字之间的映射
        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DataSourceType.DataBaseType.mysqlDataSource, primaryDataSource);
        targetDataSource.put(DataSourceType.DataBaseType.mysql2DataSource, secondaryDataSource);
        //动态数据源
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(primaryDataSource);//设置默认对象
        System.out.println("11"+dataSource);
        return dataSource;
    }
    /**
     * SQL会话工厂
     *
     * @param dataSource 数据源
     * @return 工厂
     * @throws Exception 异常
     */
    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception {
        // 资源模式冲突解决程序
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // SQL会话工厂bean
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
        sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);

        return sessionFactory.getObject();
    }

    /**
     * 平台事务管理器
     *
     * @param dataSource 数据源
     * @return 事务管理器
     */
    @Bean(name = "mysqlTransactionManager")
    @Primary
    public PlatformTransactionManager setPlatformTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}